<template>
  <div class="add-task">

    <el-button type="success" @click="clickAddTask()"><i class="el-icon-plus"></i>添加</el-button>

    <el-dialog :title="typeName" :visible.sync="dialogVisible" width="40%" :append-to-body="true" class="add-task">

      <el-form ref="apiFrom" :model="apiFrom" label-width="100px" :rules="rules">
        <el-form-item label="任务名" required prop="name">
          <el-input v-model="apiFrom.name" placeholder="请输入任务名"></el-input>
        </el-form-item>

        <el-form-item label="接口分组" prop="group">
          <el-input v-model="apiFrom.group" placeholder="请输入接口分组"></el-input>
        </el-form-item>

        <el-form-item label="接口编号">
          <el-input v-model="apiFrom.apiId" placeholder="请输入接口编号"></el-input>
        </el-form-item>

        <el-form-item label="添加人员" required>
          <el-input v-model="apiFrom.creator" placeholder="请输入添加人员"></el-input>
        </el-form-item>
        <el-form-item label="任务类型" required>
          <el-select v-model="apiFrom.type" placeholder="请选择任务类型">
            <el-option label="单用例" value="1"></el-option>
            <el-option label="业务用例" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="cron表达式" required>
          <el-input v-model="apiFrom.cron" placeholder="请输入cron表达式"></el-input>
        </el-form-item>

        <el-form-item label="任务描述">
          <el-input type="textarea" :rows="5" placeholder="请输入任务描述" v-model="apiFrom.message">
          </el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTask()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data () {

    return {

      typeName: '添加任务',
      type: 'add',

      dialogVisible: false,

      apiFrom: {},

      rules: {
        name: [
          { required: true, message: '请输入任务名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        group: [
          { required: true, message: '请输入任务分组', trigger: 'change' }
        ],

      }

    }

  },

  methods: {

    //点击添加任务按钮
    clickAddTask () {
      this.dialogVisible = true
      this.typeName = '添加任务'
      this.type = 'add'
    },


    //添加任务
    addTask (apiFrom) {

      this.$refs['apiFrom'].validate((valid) => {
        if (valid) {


          if (this.type == 'add') {

            //数据提交成功后

            this.$message({
              message: '添加成功！',
              type: 'warning'
            });

            this.$emit("searchTask1", '', '', '')

            this.dialogVisible = false


          } else if (this.type == 'edit') {

          }




        } else {
          console.log('error submit!!');
          return false;
        }
      });


    }
  }
}
</script>

<style scoped>
.add-task {
  display: inline;
}

.add-task >>> .el-select {
  display: contents;
}
</style>